package com.qdsg.ysg.doctor.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qdsg.ysg.doctor.ui.dialog.AddGroupDialog;
import com.qdsg.ysg.doctor.util.DpUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.GroupResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;

public class GroupManageActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.btn_add)
    LinearLayout btn_add;
    MyAdapter adapter;
    String id;
    List<GroupResponse.Group> list = new ArrayList<>();

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager( GroupManageActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px( GroupManageActivity.this, 16);
            }
        });
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {

        });
        checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                checkbox.setText("完成");
                for (GroupResponse.Group group : list) {
                    group.flag = true;
                }
                adapter.notifyDataSetChanged();
            } else {
                checkbox.setText("编辑");
                for (GroupResponse.Group group : list) {
                    group.flag = false;
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {
        getGroupInfo();
        id = getIntent().getStringExtra("id");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_group_manage;
    }


    @OnClick(R.id.btn_add)
    void btn_add() {
        showAddGroupDialog();
    }

    private AddGroupDialog mAddGroupDialog;

    private void showAddGroupDialog() {
        mAddGroupDialog = AddGroupDialog.newInstance();
        mAddGroupDialog.show(getSupportFragmentManager(), "mAddGroupDialog");
        mAddGroupDialog.setmListener(new AddGroupDialog.OnItemClickListener() {

            @Override
            public void onConfirm(String code) {
                getGroupInfo();
            }

            @Override
            public void onCancel() {

            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public MyAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder;
            myHolder = new MyAdapter.Holder(LayoutInflater.from( GroupManageActivity.this).inflate(R.layout.item_group, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(MyAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updatePatientGroup(id, list.get(position).id, list.get(position).groupName);
                }
            });
            holder.tv_name.setText(list.get(position).groupName);
            if (list.get(position).flag) {
                holder.btn_del.setVisibility(View.VISIBLE);
            } else {
                holder.btn_del.setVisibility(View.GONE);
            }
            holder.btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    new AlertDialog.Builder( GroupManageActivity.this).setTitle("退出")//设置对话框标题
                            .setMessage("确定要删除？")//设置显示的内容
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
                                @Override
                                public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                    delGroup(list.get(position).id);
                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//取消按钮的响应事件
                        }
                    }).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name;
            ImageView btn_del;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                btn_del = itemView.findViewById(R.id.btn_del);
            }
        }
    }

    private void getGroupInfo() {
        RestProxy.getInstance().getGroupInfo(new Observer<GroupResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(GroupResponse groupResponse) {
                list.clear();
                list.addAll(groupResponse.data);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }

    private void delGroup(String id) {
        RestProxy.getInstance().delGroup(id, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                getGroupInfo();
                checkbox.setChecked(false);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void updatePatientGroup(String tbPatientId, String id, final String groupName) {
        RestProxy.getInstance().updatePatientGroup(tbPatientId, id, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Intent mIntent = new Intent();
                mIntent.putExtra("group", groupName);
                setResult(RESULT_OK, mIntent);
                finish();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
